import { getList ,getStoreList,getAdress} from "../api/api"

//请求商品列表数据
const getList1 = () => dispatch => {
    getList()
        .then(res => {
            // console.log(res)
            // let keys = Object.keys
            let data =res.data.map(item => {
                item.foods = item.foods.map(foods => (Object.assign(foods, { count: 0 , totalPrice: 0 })))
                return {...item}
            })
            // console.log(data)
            dispatch({
                type: "GET_LIST",
                payload: data
            })
        })
}
//加号按钮
const Add = (addItem, dispatch) => {
    dispatch({
        type: "ADD",
        payload: addItem
    })
}
//减号按钮
const Del = (delItem, dispatch) => {
    dispatch({
        type: "DEL",
        payload: delItem
    })
}

// 发起gocart 动作
const Gocart=(pro,dispatch)=>{
    console.log(pro)
    dispatch({
        type:'MAKE_ORDER',
        payload:pro
    })
}

//构建封装函数
// 在这里派发action
const creator = (type, payload) => ({ type, payload })

//获取商家列表的接口
const getStoreLists =()=>dispatch=>{
    getStoreList()
    .then(res=>{
        // console.log(res)
        dispatch(creator('GET_STORE_INFO',res.data))
    })
}
const getAddress=(keyword)=>dispatch=>{
    //调用api中获取数据的方法
    console.log(1)
    getAdress(keyword)
    .then(res=>{
        console.log(res);
        dispatch(creator('GET_ADRESS',res))
    })
}

const logsubMit = (num, dispatch) => {
    console.log(num)
    dispatch({
        type: "LOGSUBMIT",
        payload: num
    })
}
export {
    getList1,
    Add,
    Del,
    Gocart,
    getStoreLists,
    getAddress,
    logsubMit
}